import { ResponseCode } from "@/common/Responsetypes";
import { reactive, computed } from "vue";
import { useUserStore } from "@/stores/useUserStore";
import _ from "lodash-es";

export interface IUserInfoRegister {
  email: string;
  password: string;
  code: string;
}

export default function () {
  const userInfo: IUserInfoRegister = reactive({
    email: "",
    password: "",
    code: "",
  });

  function HandleRegister(fromEL: { validate: () => Promise<unknown> } | null) {
    const { register } = useUserStore();

    fromEL!.validate().then(async () => {
      uni.showLoading();

      const result = await register({
        ...userInfo,
        nickname: "flaging" + _.random(1000, 9999),
      });
      if (result.code === ResponseCode.PASS) {
        uni.hideLoading();
        uni.showToast({
          title: "注册成功",
          icon: "none",
          mask: true,
        });

        uni.reLaunch({
          url: "/pages/index/index",
        });
      }
    });
  }

  const canClick = computed(() => {
    return userInfo.email && userInfo.password && userInfo.code ? true : false;
  });

  return {
    HandleRegister,
    userInfo,
    canClick,
  };
}
